package com.keggview.application.xml.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.keggview.application.xml.datatypes.Alt;
import com.keggview.application.xml.datatypes.Component;
import com.keggview.application.xml.datatypes.Entry;
import com.keggview.application.xml.datatypes.Graphics;
import com.keggview.application.xml.datatypes.Pathway;
import com.keggview.application.xml.datatypes.Product;
import com.keggview.application.xml.datatypes.Reaction;
import com.keggview.application.xml.datatypes.Relation;
import com.keggview.application.xml.datatypes.Substrate;
import com.keggview.application.xml.datatypes.Subtype;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Pablo
 *
 */
public class PathwayFactory {

	public static void main(String[] args) {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("pathway", Pathway.class);

		// set attributes for Pathway
		xstream.useAttributeFor(Pathway.class, "name");
		xstream.useAttributeFor(Pathway.class, "org");
		xstream.useAttributeFor(Pathway.class, "number");
		xstream.useAttributeFor(Pathway.class, "title");
		xstream.useAttributeFor(Pathway.class, "image");
		xstream.useAttributeFor(Pathway.class, "link");
		// delete entries, parse entry to list entries
		xstream.addImplicitCollection(Pathway.class, "entries", Entry.class);
		xstream.addImplicitCollection(Pathway.class, "reactions", Reaction.class);
		xstream.addImplicitCollection(Pathway.class, "relations", Relation.class);
		// set values for entry
		xstream.alias("entry", Entry.class);
		xstream.useAttributeFor(Entry.class, "id");
		xstream.useAttributeFor(Entry.class, "name");
		xstream.useAttributeFor(Entry.class, "type");
		xstream.useAttributeFor(Entry.class, "reaction");
		xstream.useAttributeFor(Entry.class, "link");
		xstream.useAttributeFor(Entry.class, "map");
		xstream.addImplicitCollection(Entry.class, "components", Component.class);
		// set values for Graphics
		xstream.alias("graphics", Graphics.class);
		xstream.useAttributeFor(Graphics.class, "name");
		xstream.useAttributeFor(Graphics.class, "x");
		xstream.useAttributeFor(Graphics.class, "y");
		xstream.useAttributeFor(Graphics.class, "type");
		xstream.useAttributeFor(Graphics.class, "width");
		xstream.useAttributeFor(Graphics.class, "height");
		xstream.useAttributeFor(Graphics.class, "fgcolor");
		xstream.useAttributeFor(Graphics.class, "bgcolor");

		// set values for Alt
		xstream.alias("alt", Alt.class);
		xstream.useAttributeFor(Alt.class, "name");
		
		// set values for Component
		xstream.alias("component", Component.class);
		xstream.useAttributeFor(Component.class, "id");

		// set values for Product
		xstream.alias("product", Product.class);
		xstream.useAttributeFor(Product.class, "name");
		
		// set values for Reaction
		xstream.alias("reaction", Reaction.class);
		xstream.useAttributeFor(Reaction.class, "name");
		xstream.useAttributeFor(Reaction.class, "type");
		xstream.addImplicitCollection(Reaction.class, "substrates", Substrate.class);
		xstream.addImplicitCollection(Reaction.class, "products");
		
		// set values Relation
		xstream.alias("relation", Relation.class);
		xstream.useAttributeFor(Relation.class, "entry1");
		xstream.useAttributeFor(Relation.class, "entry2");
		xstream.useAttributeFor(Relation.class, "type");
		xstream.addImplicitCollection(Relation.class, "subtypes", Subtype.class);
		
		// set values for Substrate
		xstream.alias("substrate", Substrate.class);
		xstream.useAttributeFor(Substrate.class, "name");
		
		
		// set values for Subtype
		xstream.alias("subtype", Subtype.class);
		xstream.useAttributeFor(Subtype.class, "name");
		xstream.useAttributeFor(Subtype.class, "value");


		File ff = new File("d:/mgr/maps/map00020.xml");
		FileInputStream input = null;
		try {
			input = new FileInputStream(ff);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Pathway ph = (Pathway) xstream.fromXML(input);
			System.out.println(ph.getImage());
			File fout = new File("d:/mgr/temp/out.xml");
			FileOutputStream fos = new FileOutputStream(fout);
			xstream.toXML(ph, fos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
